
//
//  NSString+Common.m
//  ArtDog
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NSString+Common.h"
#import <CommonCrypto/CommonCrypto.h>
#import "HLMacro.h"

@implementation NSString (Common)


#pragma mark - 网络接口
+ (NSString *)getURLStringWith:(NSString *)host andPaths:(id)paths andParams:(id)params
{
    NSString *urlString = host == nil ? @"": host;
    if ([paths isKindOfClass:[NSArray class]]) {
        for (NSString *path in paths) {
            urlString = [urlString stringByAppendingPathComponent:path];
        }
        urlString = [urlString stringByAppendingString:@"?"];
    } else if ([paths isKindOfClass:[NSString class]]) {
        urlString = [urlString stringByAppendingString:paths];
        urlString = [urlString stringByAppendingString:@"?"];
    }
    
    if ([params isKindOfClass:[NSDictionary class]]) {
        for (NSString *param in [params allKeys]) {
            if (![[urlString substringFromIndex:urlString.length - 1] isEqualToString:@"?"]) {
                urlString = [urlString stringByAppendingString:@"&"];
            }
            urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"%@=", param]];
            urlString = [urlString stringByAppendingString:[params objectForKey:param]];
        }
    } else if ([params isKindOfClass:[NSString class]]) {
        urlString = [urlString stringByAppendingString:params];
    } else {
        urlString = [urlString substringToIndex:urlString.length - 1];
    }
    
    return urlString;
}

#pragma mark - 加密
- (NSString *)stringWithMD5
{
    const char *cString = [self UTF8String];
    CC_LONG length = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cString, length, bytes);
    // 将c字符串转换成oc字符串
    NSMutableString *string = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [string appendFormat:@"%02x", bytes[i]];
    }
    return string;
}

- (NSString *)stringWith32BitMD5Upper
{
    return [[self stringWithMD5]uppercaseString];
}

- (NSString *)stringWith32BitMD5Lower
{
    return [[self stringWithMD5]lowercaseString];
}

- (NSString *)stringWith16BitMD5Upper
{
    NSRange range = NSMakeRange(8, 16);
    return [[[self stringWithMD5]substringWithRange:range]uppercaseString];
}

- (NSString *)stringWith16BitMD5Lower
{
    NSRange range = NSMakeRange(8, 16);
    return [[[self stringWithMD5]substringWithRange:range]lowercaseString];
}


#pragma mark -SHA_1
- (NSString *)stringWithSHA_1
{
    const char *cString = [self UTF8String];
    CC_LONG len = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(cString, len, bytes);
    NSMutableString *string = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH];
    for (int i = 0; i< CC_SHA1_DIGEST_LENGTH; i++) {
        [string appendFormat:@"%02x", bytes[i]];
    }
    return string;
}

#pragma mark - Base64
/**
 * 1.Base64 编码格式 一般应用于少量数据的编码
 *
 **/

/** 编码 */
- (NSString *)stringWithBase64
{
    NSData *stringData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [stringData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    
    return base64String;
}

- (NSString *)base64WithString
{
    NSData *base64Data = [[NSData alloc]initWithBase64EncodedString:self options:NSDataBase64DecodingIgnoreUnknownCharacters];
    NSString *orginString = [[NSString alloc]initWithData:base64Data encoding:NSUTF8StringEncoding];
    return orginString;
}


/** 得到UserDefault */

+ (NSString *)getUserDefaultId
{
    
    return [[NSString getUserDefaultsDic] objectForKey:@"id"];
}

+ (NSString *)getUserDefaultToken
{
    return [[NSString getUserDefaultsDic] objectForKey:@"token"];
}

+ (NSDictionary *)getUserDefaultsDic
{
    return [[NSUserDefaults standardUserDefaults]objectForKey:kUserDefaultPathKey];
}


/** 计算文字高度 */
- (CGFloat)calculateWordsHeightWithFontSize:(CGFloat)size andWidth:(CGFloat)width
{
    CGRect rect = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:size]} context:nil];
    
    return ceilf(CGRectGetHeight(rect));
}

/** 时间处理 */
- (NSString *)dealDate
{
    NSDate *date = [NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    if (![[self substringToIndex:4] isEqualToString:[dateString substringToIndex:4]]) {
        return [self substringToIndex:10];
    } else if (![[self substringToIndex:7] isEqualToString:[dateString substringToIndex:7]]) {
        NSInteger currentDate = [[[dateString substringToIndex:7]substringFromIndex:5]integerValue];
        NSInteger selfDate = [[[self substringToIndex:7]substringFromIndex:5]integerValue];
        return [NSString stringWithFormat:@"%ld月前", currentDate - selfDate];
    } else if (![[self substringToIndex:10] isEqualToString:[dateString substringToIndex:10]]) {
        NSInteger currentDate = [[[dateString substringToIndex:10]substringFromIndex:8]integerValue];
        NSInteger selfDate = [[[self substringToIndex:10]substringFromIndex:8]integerValue];
        NSInteger currentDateHour = [[[dateString substringToIndex:13]substringFromIndex:11]integerValue];
        NSInteger selfDateHour = [[[self substringToIndex:13]substringFromIndex:11]integerValue];
        NSInteger day = currentDate - selfDate;
        NSInteger hour = currentDateHour - selfDateHour;
        if (day == 1 && hour < 0) {
            return [NSString stringWithFormat:@"%ld小时前", hour * (-1)];
        } else {
            return hour >= 0 ?[NSString stringWithFormat:@"%ld天前", day]: [NSString stringWithFormat:@"%ld天前", day - 1];
        }
    } else if (![[self substringToIndex:13] isEqualToString:[dateString substringToIndex:13]]) {
        NSInteger currentDate = [[[dateString substringToIndex:13]substringFromIndex:11]integerValue];
        NSInteger selfDate = [[[self substringToIndex:13]substringFromIndex:11]integerValue];
        NSInteger currentDateMin = [[[dateString substringToIndex:16]substringFromIndex:14]integerValue];
        NSInteger selfDateMin = [[[self substringToIndex:16]substringFromIndex:14]integerValue];
        NSInteger hour = currentDate - selfDate;
        NSInteger min = currentDateMin - selfDateMin;
        if (hour == 1 && min < 0) {
            return [NSString stringWithFormat:@"%ld分钟前", min * (-1)];
        } else {
            return min >= 0 ?[NSString stringWithFormat:@"%ld小时前", hour]: [NSString stringWithFormat:@"%ld小时前", hour - 1];
        }
    } else if (![[self substringToIndex:16] isEqualToString:[dateString substringToIndex:16]]) {
        NSInteger currentDate = [[[dateString substringToIndex:16]substringFromIndex:14]integerValue];
        NSInteger selfDate = [[[self substringToIndex:16]substringFromIndex:14]integerValue];
        NSInteger currentDateSec = [[[dateString substringToIndex:19]substringFromIndex:17]integerValue];
        NSInteger selfDateSec = [[[self substringToIndex:19]substringFromIndex:17]integerValue];
        NSInteger min = currentDate - selfDate;
        NSInteger sec = currentDateSec - selfDateSec;
        if (min == 1 && sec < 0) {
            return [NSString stringWithFormat:@"%ld秒前", sec * (-1)];
        } else {
            return sec >= 0 ?[NSString stringWithFormat:@"%ld分钟前", min]: [NSString stringWithFormat:@"%ld分钟前", min - 1];
        }
    } else {
        NSInteger currentDate = [[[dateString substringToIndex:19]substringFromIndex:17]integerValue];
        NSInteger selfDate = [[[self substringToIndex:19]substringFromIndex:17]integerValue];
        return [NSString stringWithFormat:@"%ld秒前", currentDate - selfDate];
    }
}


@end
